package com.hayato.demo.client.Info;

import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.hayato.demo.client.model.OrderProductModel;
import com.hayato.demo.utils.PMF;

@PersistenceCapable
public class OrderProductInfo {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	@Persistent
	private long order_id;
	@Persistent
	private long product_id;
	@Persistent
	private int product_amount;
	@Persistent
	private boolean lack_flg;
	@Persistent
	private String remark;
	@Persistent
	private String date_created;
	@Persistent
	private String date_updated;
	
	public OrderProductInfo(){
		
		Date today = new Date();
		
		lack_flg = true;
		date_created = today.toString();
		date_updated = today.toString();
	}
	public OrderProductInfo(OrderProductModel model){
		setOrder_id(model.getOrder_id());
		setProduct_id(model.getProductModel().getId());
		setProduct_amount(model.getProduct_amount());
		setLack_flg(model.isLack_flg());
		setRemark(model.getRemark());
	}
	public long getId() {
		return id;
	}

	public void setOrder_id(long order_id) {
		this.order_id = order_id;
	}

	public long getOrder_id() {
		return order_id;
	}

	public void setProduct_amount(int product_amount) {
		this.product_amount = product_amount;
	}

	public int getProduct_amount() {
		return product_amount;
	}

	public void setDate_created(String date_created) {
		this.date_created = date_created;
	}

	public String getDate_created() {
		return date_created;
	}

	public void setProduct_id(long product_id) {
		this.product_id = product_id;
	}

	public long getProduct_id() {
		return product_id;
	}

	public void setDate_updated(String date_updated) {
		this.date_updated = date_updated;
	}

	public String getDate_updated() {
		return date_updated;
	}

	public boolean isLack_flg() {
		return lack_flg;
	}

	public void setLack_flg(boolean lack_flg) {
		this.lack_flg = lack_flg;
	}
	
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public OrderProductModel toModel(){
		OrderProductModel orderProModel=new OrderProductModel();
		orderProModel.setId(getId());
		orderProModel.setOrder_id(getOrder_id());
		orderProModel.setLack_flg(isLack_flg());
		orderProModel.setProduct_amount(getProduct_amount());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ProductInfo proInfo;
		try {
			proInfo = pm.getObjectById(ProductInfo.class,getProduct_id());
		} catch (Exception e) {
			proInfo = null;
		}
		orderProModel.setProductModel(proInfo.toModel());
		
		OrderInfo orderInfo;
		try {
			orderInfo = pm.getObjectById(OrderInfo.class,getOrder_id());
		} catch (Exception e) {
			orderInfo = null;
		}
		orderProModel.setFinishDate(orderInfo.getShop_finish_date());
		orderProModel.setOrderDate(orderInfo.getOrder_date());
		orderProModel.setRemark(getRemark());
		return orderProModel;
	}
}
